package base.view.ui

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.widget.SeekBar
import java.util.Collections.rotate
import android.view.MotionEvent




/**
 * Created by jjw on 2018/6/8 0008.
 */
class VerticalSeekBar : SeekBar {

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(h, w, oldh, oldw)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec)
        setMeasuredDimension(measuredHeight, measuredWidth)
    }

    override fun onDraw(c: Canvas) {
        //将SeekBar转转90度
        c.rotate(-90f)
        //将旋转后的视图移动回来
        c.translate(-height.toFloat(), 0f)
        super.onDraw(c)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (!isEnabled) {
            return false
        }

        when (event.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP -> {
                var i = 0
                //获取滑动的距离
                i = max - (max * event.y / height).toInt()
                //设置进度
                progress = i
                //每次拖动SeekBar都会调用
                onSizeChanged(width, height, 0, 0)
            }

            MotionEvent.ACTION_CANCEL -> {
            }
        }
        return true
    }
}
